#include "client.h"

#include <grpcpp/grpcpp.h>

TestClient::TestClient(std::shared_ptr<grpc::Channel> channel) :
    stub_(test::idl::Test::NewStub(std::dynamic_pointer_cast<grpc::ChannelInterface>(channel)))
{
}

std::string TestClient::getMessage(const std::string &message)
{
    test::idl::Request request;
    request.set_message(message);
    test::idl::Response response;
    grpc::ClientContext context;

    // Here we can use the stub's newly available method we just added.
    grpc::Status status = stub_->getMessage(&context, request, &response);
    if (!status.ok()) {
        std::cout << status.error_code() << ": " << status.error_message() << std::endl;
        return "RPC failed";
    }
    return response.database_ip();
}